// pages/edit_message/edit_message.js
var Trim = require('../../utils/util.js');
var idReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
var app;
var url;
var headers, initInfo, index;
Page({
  data:{
    url:"https://app1.sztravel.com.cn/file/weixinapp/img",
    isman:true,
    index: 0,
    userinfo: {
      birthday: "",
      card_no: "",
      card_type: "sfz",
      gender: "M",
      head_img:"",
      my_mavin:{},
      phone_num: "",
      real_name:"",
      user_id:null,    
    },
    isActive: false,//是否可以提交开关
    card_sort: ["身份证", "护照", "港澳通行证", "台湾通行证"],
  },
  sexselect: function (e) {
    var sex = e.target.dataset.sex;
    var info = this.data.userinfo;
    info.gender = sex
    this.setData({
      userinfo: info
    })
    this.isActive()
  },
  showMode: function (mes) {
    wx.showModal({
      title: '来自伴行的提示',
      content: mes,
      showCancel: false
    })
  },
  goToChange(){
    var that=this;
    var info=this.data.userinfo;
    console.log(info);
    if(this.data.isActive){//暂定可以提交 判空
      if (info.card_type == "sfz" && !idReg.test(info.card_no)){
        that.showMode("身份证格式有误");
        return;
      }


      var req = '{"head_img":"","real_name":"'
        + info.real_name + '","gender":"'
        + info.gender + '","birthday":"'
        + info.birthday + '","phone_num":"'
        + info.phone_num + '","card_type":"'
        + info.card_type + '","card_no":"'
        + info.card_no + '"}'
      wx.request({
        url: url +'api/modify_user_profile',
        method:"POST",
        header:headers,
        data:{
          "req":encodeURIComponent(req)
        },
        success:function(data){
          console.log(data);
          wx.showModal({
            title: '来自伴行的信息',
            content: data.data.message,
            showCancel:false,
            success:function(){
              if (data.data.result == 1) {//修改成功
                try {//同步本地缓存以保证my页面个人信息名字同步更改
                  var inf = JSON.parse(wx.getStorageSync("infor"));
                  inf.nick_name = info.real_name;
                  wx.setStorageSync('infor', JSON.stringify(inf));
                } catch (e) { }


                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
      })
    }
  },
  isActive: function () {//判断提交按钮是否激活
    var isActive = this.data.isActive
    var info = this.data.userinfo;
    console.log(JSON.stringify(info) == JSON.stringify(initInfo))
    if (JSON.stringify(initInfo) != JSON.stringify(info)) {//暂定可以提交
      isActive = true;
    } else {
      isActive = false;
    }
    this.setData({
      isActive: isActive
    })
  },
  bindPickerChange: function (e) {
    console.log(e.detail.value)
    var info = this.data.userinfo;
    switch (parseInt(e.detail.value)) {
      case 0:
        info.card_type = "sfz"; break;
      case 1:
        info.card_type = "passport"; break;
      case 2:
        info.card_type = "ga"; break;
      case 3:
        info.card_type = "tw"; break;
    };
    this.setData({
      index: e.detail.value,
      userinfo: info
    })
    console.log(this.data)
    this.isActive()
    // console.log(this.data.userinfo)
  },
  fillter: function (e) {
    var cdata = this.data.userinfo;
    // if(e.target.dataset.i=="sex"){
    //   cdata[e.target.dataset.i]=this.data.isman?"男":"女";
    // }else{
    //   cdata[e.target.dataset.i]=e.detail.value;
    // }
    // 
    // if(cdata.passport || cdata.id || cdata.gaid){
    //   cdata.isSave = true;
    // }else{
    //   cdata.isSave = false;
    // }
    console.log(e)
    // if(e.target.dataset.i=="tel"){//更改手机号码

    // }
    switch (e.target.dataset.i) {
      case "name":
        cdata.real_name = e.detail.value; break;
      case "selectDate":
        cdata.birthday = e.detail.value; break;
      case "tel"://更改手机号码
        cdata.phone_num = e.detail.value; break;
      case "card_no"://更改证件号码
        cdata.card_no = e.detail.value; break;
    };
    this.setData({
      userinfo: cdata
    })
    this.isActive()
    console.log(this.data.userinfo);
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    
    // 页面显示
    var that=this;
    app = getApp();
    url = app.globalData.url;
    headers = app.globalData.headers;
    wx.request({
      url: url +'api/get_user_profile',
      header:headers,
      success:function(data){
        console.log(data)
        if(data.data.result===1){//成功
          switch (data.data.data.card_type) {
            case "sfz":
              index = 0; break;
            case "ga":
              index = 2; break;
            case "tw":
              index = 3; break;
            case "passport":
              index = 1;
          }
          initInfo = data.data.data;
          that.setData({
            userinfo: data.data.data,
            index: index
          })
         
          
        }else{
          wx.showModal({
            title: '来自伴行的提示',
            content: data.data.message
          })
        }
        console.log(that.data)
      }
    })
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})